package com.ruoyi.project.module.api.pay.alipay.controller;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.project.module.api.annotation.Login;
import com.ruoyi.project.module.api.annotation.LoginUser;
import com.ruoyi.project.module.api.common.utils.ApiResult;
import com.ruoyi.project.module.api.pay.alipay.service.AlipayService;
import com.ruoyi.project.module.member.domain.Member;
import com.ruoyi.project.module.order.domain.Order;
import com.ruoyi.project.module.order.service.impl.OrderServicePlusImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/pay/alipay")
@Api("支付宝支付接口")
public class AlipayController {

    private static Logger logger = LoggerFactory.getLogger(AlipayController.class);

    @Autowired
    private AlipayService alipayService;

    @Autowired
    private OrderServicePlusImpl orderService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 预创建-支付宝订单
     * @param member
     * @param orderType
     * @param feesetId
     * @param point
     * @return
     */
    @Login
    @RequestMapping(value = "/createOrder", method = RequestMethod.GET)
    @ApiOperation("创建支付宝订单")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true)})
    public ApiResult createAlipayOrder(
            @LoginUser Member member,
            @RequestParam(value = "orderType") String orderType,
            @RequestParam(value = "feesetId",required = false) String feesetId,
            @RequestParam(value = "point",required = false) Integer point) {
        String orderKey = alipayService.createOrder(member, orderType, feesetId, point);
        Map<String, Object> result = new HashMap<>();
        result.put("qrCode", serverConfig.getUrl() + "/api/pay/alipay/createCode?orderKey=" + orderKey);
        result.put("expire", System.currentTimeMillis() / 1000 + 10 * 60);
        return ApiResult.ok(result);
    }

    /**
     * 生成支付宝支付二维码
     *
     * @param response
     * @param orderKey
     */
    @RequestMapping(value = "/createCode", method = RequestMethod.GET)
    public void createCode(HttpServletResponse response, @RequestParam("orderKey") String orderKey) {
        try {
            String qrCode = alipayService.createCode(orderKey);
            OutputStream ops = response.getOutputStream();
            QrCodeUtil.generate(qrCode, 300, 300, ImgUtil.IMAGE_TYPE_PNG, ops);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 支付宝回调
     *
     * @param request
     */
    @RequestMapping("/notify")
    public void notify(HttpServletRequest request) {
        Map map = request.getParameterMap();
        logger.info("【支付宝回调参数】：" + JSONUtil.parse(map));
        String tradeStatus = request.getParameter("trade_status");
        String outTradeNo = request.getParameter("out_trade_no");
        String tradeNo = request.getParameter("trade_no");
        alipayService.dealOrder(tradeStatus, outTradeNo, tradeNo);
    }
}
